package com.soup.lab.redis;

import org.junit.jupiter.api.*;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.params.SetParams;

import java.util.Arrays;

public class JedisUseCase {

    static JedisCluster jedis = new JedisCluster(new HostAndPort("192.168.1.5", 46379));

    static final String OK = "OK";

    @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
    static class StringUseCase {

        String key = "jedis:test";
        String val = "hello";
        String val2 = "holla";

        @Test
        @Order(1)
        public void testSet() {
            Assertions.assertEquals(OK, jedis.set(key, val));
        }

        @Test
        @Order(2)
        public void testGet() {
            Assertions.assertEquals(val, jedis.get(key));
        }

        @Test
        @Order(3)
        public void testDel() {
            Assertions.assertEquals(1, jedis.del(key));
            Assertions.assertNull(jedis.get(key));
        }

        @Test
        @Order(4)
        public void testExpire() {
            jedis.setex(key, 1L, val2);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ignore) {
            }
            Assertions.assertNull(jedis.get(key));
        }

        @Test
        @Order(5)
        public void testSetnx() {
            Assertions.assertEquals(1, jedis.setnx(key, val2));
        }

        @Test
        @Order(6)
        public void testGetSet() {
            Assertions.assertEquals(val2, jedis.getSet(key, val));
        }

        @Test
        @Order(7)
        public void testSetParam() {
            testDel();
            Assertions.assertEquals(null, jedis.set(key, val2, SetParams.setParams().xx()));
            testSet();
            Assertions.assertEquals(OK, jedis.set(key, val2, SetParams.setParams().xx()));
        }

        @Test
        @Order(8)
        public void testSetnxex() {
            Assertions.assertEquals(OK, jedis.set(key, val, SetParams.setParams().nx().ex(10L)));
            Assertions.assertTrue(jedis.ttl(key) > 0);
            Assertions.assertNull(jedis.set(key, val, SetParams.setParams().nx().ex(10L)));
        }

        /*
        目前测试redis版本不支持 keepttl命令
        @Test
        @Order(9)
        public void testSetKeepTTL() {
            jedis.setex(key, 10L, val);
            Assertions.assertTrue(jedis.ttl(key) > 0);
            jedis.set(key, val2);
            // 在重新设置值后，有效期重置了
            Assertions.assertEquals(-1, jedis.ttl(key));

            jedis.setex(key, 10L, val);
            Assertions.assertTrue(jedis.ttl(key) > 0);
            jedis.set(key, val2, SetParams.setParams().keepttl());
            Assertions.assertTrue(jedis.ttl(key) > 0);
        }*/

        @Test
        @Order(10)
        public void testMset() {
            Assertions.assertEquals(OK, jedis.mset(key, val, key + ":1", val2));
            Assertions.assertLinesMatch(Arrays.asList(val, val2), jedis.mget(key, key + ":1"));
        }

    }


}
